// This File is part of the "nyaqt" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using EnvDTE80;
using Microsoft.Win32;

namespace SmAddin.Commands
{
    class CommandPromptCommand
    {
        private const string VS90COMNTOOLS = "VS{0}COMNTOOLS";
        private const string cmd = "cmd.exe";
        private const string vsvars32 = "vsvars32.bat";


        DTE2 _dte;

        public DTE2 DTE
        {
            get { return _dte; }
            set { _dte = value; }
        }

        public void Exec()
        {
            object[] selItems = DteHelper.SelectedItemsInSolutionExplorer(DTE);
            if (selItems.Length > 0)
            {
                string workingDir = Path.GetDirectoryName(DteHelper.GetHierarchyItemPath(selItems[selItems.Length - 1]));
                Directory.SetCurrentDirectory(workingDir);
            }
            else
                Directory.SetCurrentDirectory("C:\\");

            string vsvars32Path = GetVsVarsPath(DTE);

            int i = 0;
            int max = 10;
            char c1 = 'A';

            string bat = Path.Combine(Path.GetTempPath(), "selvars.bat");
            if (File.Exists(bat))
                File.Delete(bat);
            using (StreamWriter selvars = File.CreateText(bat))
            {
                selvars.WriteLine("@call \"" + vsvars32Path + "\"");
                selvars.WriteLine("@prompt $M$P$_%username%$G");
                selvars.WriteLine("@echo.");
                string all1 = "";
                string all2 = "";
                string all3 = "";
                for (int index = selItems.Length - 1; index >= 0 && index < max; index--)
                {
                    object item = selItems[index];

                    char c = (char)(((int)c1) + i);
                    string itempath = DteHelper.GetHierarchyItemPath(item);
                    selvars.WriteLine("@set " + c + "=\"" + itempath + "\"");
                    selvars.WriteLine("@echo set " + c + "=\"" + itempath + "\"");
                    all1 += " " + "\"" + itempath + "\"";
                    all2 += "," + "\"" + itempath + "\"";
                    all3 += ";" + "\"" + itempath + "\"";

                    i += 1;
                }

                selvars.WriteLine("@set ALL1=" + all1);
                selvars.WriteLine("@set ALL2=" + all2);
                selvars.WriteLine("@set ALL3=" + all3);
                selvars.WriteLine("@echo set ALL1=A B C ...");
                selvars.WriteLine("@echo set ALL2=A,B,C,...");
                selvars.WriteLine("@echo set ALL3=A;B;C;...");
            }
            if (File.Exists(vsvars32Path))
            {
                string args = "/k \"" + bat +"\"";
                System.Diagnostics.Process.Start(cmd, args);
            }
        }

        public static string GetVsPath(DTE2 dte)
        {
            string version = dte.Version.Substring(0, 1) + "0";
            string vsPath = "";

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(dte.RegistryRoot))
            {
                vsPath = key.GetValue("InstallDir").ToString();
            }
            return vsPath;
        }

        static string GetVsVarsPath(DTE2 dte)
        {
            string version = dte.Version.Substring(0, 1) + "0";
            string vsvars32Path;
            string vstools = string.Format(VS90COMNTOOLS, version);

            if (Environment.GetEnvironmentVariable(vstools) != null)
            {
                vsvars32Path = Path.Combine(
                    Environment.GetEnvironmentVariable(vstools),
                    vsvars32);
            }
            else
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(dte.RegistryRoot))
                {
                    string installDir = key.GetValue("InstallDir").ToString();

                    vsvars32Path = Path.Combine(
                        string.Concat(installDir, @"\..\Tools\"),
                        vsvars32);
                }
            }
            return vsvars32Path;
        }
    }
}
